package cn.lichenfei.fxui.controls;

import cn.lichenfei.fxui.common.FxUtil;
import cn.lichenfei.fxui.controls.skin.CFSwitchSkin;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.css.*;
import javafx.scene.control.Control;
import javafx.scene.paint.Color;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CFSwitch extends Control {

    private static final String STYLE_SHEET = FxUtil.getResource("/css/cf-switch.css");
    private static final String STYLE_CLASS = "cf-switch";
    private static final PseudoClass PSEUDO_CLASS_SELECTED = PseudoClass.getPseudoClass("selected");
    //
    private BooleanProperty selected = new SimpleBooleanProperty(false);
    private StyleableObjectProperty<Color> fillColor = new SimpleStyleableObjectProperty<>(StyleableProperties.FILL_COLOR);
    private StringProperty text = new SimpleStringProperty();

    public CFSwitch() {
        initialize();
    }

    public CFSwitch(boolean value) {
        initialize();
        selected.set(value);
    }

    public CFSwitch(boolean value, String text) {
        initialize();
        selected.set(value);
        this.text.set(text);
    }

    private void initialize() {
        setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
        getStyleClass().add(STYLE_CLASS);
        selected.addListener((observable, oldValue, newValue) -> this.pseudoClassStateChanged(PSEUDO_CLASS_SELECTED, newValue));
    }

    public BooleanProperty selectedProperty() {
        return selected;
    }

    public StyleableObjectProperty<Color> fillColorProperty() {
        return fillColor;
    }

    public boolean isSelected() {
        return selected.get();
    }

    public String getText() {
        return text.get();
    }

    public StringProperty textProperty() {
        return text;
    }

    /**
     * 自定义CSS属性
     */
    private static class StyleableProperties {
        private static final List<CssMetaData<? extends Styleable, ?>> CSS_META_DATA_LIST;

        private static final CssMetaData<CFSwitch, Color> FILL_COLOR = new CssMetaData<>("-cf-fill-color", new StyleConverter<>(), Color.TRANSPARENT) {
            @Override
            public boolean isSettable(CFSwitch styleable) {
                return styleable.fillColor == null || !styleable.fillColor.isBound();
            }

            @Override
            public StyleableProperty<Color> getStyleableProperty(CFSwitch cfSwitch) {
                return cfSwitch.fillColor;// 返回一个StyleableProperty，其他地方绑定这个树形即可
            }
        };

        static {
            final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<CssMetaData<? extends Styleable, ?>>(Control.getClassCssMetaData());
            Collections.addAll(styleables, FILL_COLOR);
            CSS_META_DATA_LIST = Collections.unmodifiableList(styleables);
        }

    }

    public static List<CssMetaData<? extends Styleable, ?>> getControlCssMetaDataList() {
        return StyleableProperties.CSS_META_DATA_LIST;
    }

    @Override
    protected CFSwitchSkin createDefaultSkin() {
        return new CFSwitchSkin(this);
    }

    @Override
    public String getUserAgentStylesheet() {
        return STYLE_SHEET;
    }

    @Override
    public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
        return CFSwitch.getControlCssMetaDataList();
    }

}
